<?php
/**
 * Plugin Name: GPP Welcome Message Widget
 * Plugin URI: http://graphpaperpress.com/plugins/gpp-welcome-message-widget/
 * Description: A widget for adding a welcome message to your site.
 * Version: 1.0
 * Author: Thad Allender
 * Author URI: http://graphpaperpress.com
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

/**
 * Add function to widgets_init that'll load our widget.
 * @since 1.0
 */
add_action( 'widgets_init', 'gpp_welcome_load_widgets' );

/**
 * Register our widget.
 * 'GPP_Welcome_Widget' is the widget class used below.
 *
 * @since 1.0
 */
function gpp_welcome_load_widgets() {
	register_widget( 'GPP_Welcome_Widget' );
}

/**
 * Example Widget class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 1.0
 */
class GPP_Welcome_Widget extends WP_Widget {

	/**
	 * Widget setup.
	 */
	function GPP_Welcome_Widget() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'Welcome Message', 'description' => __('A widget for adding a welcome message to your site.', 'welcome_message') );

		/* Widget control settings. */
		$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'welcome-message-widget' );

		/* Create the widget. */
		$this->WP_Widget( 'welcome-message-widget', __('Welcome Message', 'welcome_message'), $widget_ops, $control_ops );
	}

	/**
	 * How to display the widget on the screen.
	 */
	function widget( $args, $instance ) {
		extract( $args );

		/* Our variables from the widget settings. */
		$title = apply_filters('widget_title', $instance['title'] );
		$message = $instance['message'];

		/* Before widget (defined by themes). */
		echo $before_widget;
		
		echo '<div class="welcomebox entry">';

		/* Display the widget title if one was input (before and after defined by themes). */
		if ( $title )
			echo "<h2>" . $title . "</h2>";
			
		/* If there is welcome text, show it. */
		if ( $message )
			echo "<h3>" . $message . "</h3>";
					
		echo '</div><!-- .welcomebox entry -->';

		/* After widget (defined by themes). */
		echo $after_widget;
	}

	/**
	 * Update the widget settings.
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );
		
		/* No need to strip tags for show_email, show_phone. */
		$instance['message'] = $new_instance['message'];
		
		return $instance;
	}

	/**
	 * Displays the widget settings controls on the widget panel.
	 * Make use of the get_field_id() and get_field_name() function
	 * when creating your form elements. This handles the confusing stuff.
	 */
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => __('', 'welcome_message'), 'message' => '' );
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<!-- Widget Title: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'welcome_message'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" />
		</p>
		
		<!-- Message: Text Area -->
		<p>
			<label for="<?php echo $this->get_field_id( 'message' ); ?>"><?php _e('Welcome Message:', 'welcome_message'); ?></label> 
			<textarea rows="6" class="widefat" id="<?php echo $this->get_field_id( 'message' ); ?>" name="<?php echo $this->get_field_name( 'message' ); ?>"><?php echo $instance['message']; ?></textarea>
		</p>
		
	<?php
	}
}

?>